<?php
/**
 * Script file
 * @author Serguei Shimansky <privateteacher@mail.ru>
 * @copyright Copyright (c) 2011, Serguei Shimansky
 * @version 1.0
 * @package fast-comment
 */

function lib_global2_prepare_str($s) {
	return $s = str_replace(array("|", "\n", "\r", "\t", "\v", "\0", "\x0B", '"'), "", trim($s));
}

function lib_global2_safe_str($s) {
	return str_replace(array("\n", "\r", "\t", "\v", "\0", "\x0B"), '', preg_replace("/[^\x20-\xFF]/", "", trim(@strval($s))));
}

function lib_global2_get_post($k, $v = '') {
	if (isset ($_GET[$k]) || isset ($_POST[$k])) {
		$v = isset ($_GET[$k]) ? $_GET[$k] : $_POST[$k];
		if (is_array($v)) {
			foreach ($v as &$v1) {
				$v1 = urldecode($v1);
				$v1 = strtr($v1, array_flip(get_html_translation_table(HTML_ENTITIES)));
				$v1 = trim($v1);
				$v1 = lib_global2_safe_str($v1);
			}
			unset($v1);
		} else {
			$v = urldecode($v);
			$v = strtr($v, array_flip(get_html_translation_table(HTML_ENTITIES)));
			$v = trim($v);
			$v = lib_global2_safe_str($v);
		}
	}
	return $v;
}

function lib_global2_fix_str_for_js($s) {
	$s = str_replace("'", '\&#39;', $s);
	return $s;
}

function lib_global2_js_unescape($s) {
	return preg_replace('/\\\u([0-9a-f]{4})/ie', "chr(hexdec('\\1'))", $s);
}

function lib_global2_ensure_amp($s) {
	$s = str_replace('&', '&amp;', $s);
	$s = str_replace(array('&amp;amp;', '&amp;#'), array('&amp;', '&#'), $s);
	$s = preg_replace("/(\&amp\;)([a-z0-9]+)(\;)/ei", "'&'.('\\2').';'", $s);
	$s = preg_replace("/(\&\#)([a-z0-9]+)(\;)/ei", "'&#'.('\\2').';'", $s);
	return $s;
}

function lib_global2_ensure_lt_gt($s) {
	$s = str_replace('<', '&lt;', $s);
	$s = str_replace('>', '&gt;', $s);
	$s = str_replace(array('&amp;lt;', '&amp;gt;'), array('&lt;', '&gt;'), $s);
	return $s;
}

function lib_global2_cut_last_160_160($s) {
	$s = preg_replace("/\&\#160\;\&\#160\; \r\n$/", '', $s);
	$s = preg_replace("/\&\#160\;\&\#160\; \n$/", '', $s);
	return $s;
}

function lib_global2_cut_last_160_183($s) {
	$s = preg_replace("/\&\#160\;\&\#183\; \r\n$/", '', $s);
	$s = preg_replace("/\&\#160\;\&\#183\; \n$/", '', $s);
	return $s;
}

function lib_global2_cut_last_br($s) {
	$s = preg_replace("/\<br \/\>\r\n$/i", '', $s);
	$s = preg_replace("/\<br \/\>\n$/i", '', $s);
	return $s;
}

function lib_global2_ord_space($s) {
	return trim(preg_replace("/[\ ]+/", " ", $s));
}

function lib_global2_ord_underscore($s) {
	return trim(preg_replace("/[\_]+/", "_", $s));
}

function lib_global2_remove_tags($s) {
	$s = stripslashes($s);
	$s = preg_replace("'<script[^>]*?>.*?</script>'si", ' ', $s);
	$s = preg_replace("'<[\/\!]*?[^<>]*?>'si", ' ', $s);
	$s = lib_global2_ord_space($s);
	return $s;
}

function lib_global2_remove_ents($s) {
	$s = preg_replace("/(\&)([A-Za-z0-9\#]+)(\;)/ei", "''", $s);
	return $s;
}

function lib_global2_remove_comments($s) {
	$s = preg_replace("/(\<!--)(.*?)(-->)/ie", "", $s);
	return $s;
}

function lib_global2_has_http($s, $r = false) {
	if (preg_match("/^(http|https|ftp)\:\/\//", $s) &&
		!preg_match("/^\//", $s)) {
		return $r = true;
	}
}

function lib_global2_remove_bbcoded($s) {

	$a = array(
		'(\[img\])(.*?)(\[\/img\])' => '',
		'(\[img\=left\])(.*?)(\[\/img\])' => '',
		'(\[img\=right\])(.*?)(\[\/img\])' => '',
		'(\[color\=[a-zA-Z0-9\#]+\])(.*?)(\[\/color\])' => '',
		'(\[)(.*?)(\])' => ''
	);

	foreach($a as $k => $v) {
		$s = preg_replace("/${k}/", $v, $s);
	}

	return $s;
}

function lib_global2_clean_title($s) {
   	$s = str_replace('&#160;', ' ', $s);
	$s = str_replace('&nbsp;', ' ', $s);
	$s = str_replace('\'', '&#39;', $s);
	$s = str_replace('№', '&#x2116;', $s);
	$s = str_replace('“', '&#171;;', $s);
	$s = str_replace('”', '&#187;', $s);
	$s = str_replace('—', '&#8212;', $s);
	$s = lib_global2_remove_tags($s);
	$s = lib_global2_ensure_lt_gt($s);
	$s = str_replace(array("\n", "\r", "\t", "\v", "\0", "\x0B"), '', preg_replace("/[^\x20-\xFF]/", " ", trim(@strval($s))));
	$s = lib_global2_ord_space($s);
	$s = lib_global2_ensure_amp($s);
	return $s;
}

function lib_global2_clean_xhtml($s) {
	$s = strval($s);

	$a = array(
'¦' => '&#166;',
'§' => '&#167;',
'©' => '&#169;',
'«' => '&#171;',
'®' => '&#174;',
'°' => '&#176;',
'±' => '&#177;',
'·' => '&#183;',
'»' => '&#187;',
'Ё' => 'Е',
'ё' => 'е',
'—' => '&#8212;',
'‘' => '&#8216;',
'’' => '&#8217;',
'“' => '&#147;',
'“' => '&#171;;',
'”' => '&#148;',
'”' => '&#187;',
'„' => '&#132;',
'•' => '&#149;',
'‰' => '%',
'№' => '&#8470;',
'™' => '&#153;',
'\'' => '&#39;',
'`' => '&#39;',
' & ' => ' &amp; ',
'&bull;' => '&#8226;',
'&cent;' => '&#162;',
'&copy;' => '&#169;',
'&darr;' => '&#8595;',
'&eacute;' => '&#233;',
'&euro;' => '&#8364;',
'&iexcl;' => '&#161;',
'&laquo;' => '&#171;',
'&ldquo;' => '&#8220;',
'&mdash;' => '&#8212;',
'&middot;' => '&#183;',
'&nbsp;' => '&#160;',
'&pound;' => '&#163;',
'&raquo;' => '&#187;',
'&rdquo;' => '&#8221;',
'&reg;' => '&#174;',
'&trade;' => '&#8482;',
'&yen;' => '&#165;',
'(c)' => '&#169;',
'NN' => '&#x2116;'
	);
	foreach ($a as $k => $v) {
		$s = str_replace($k, $v, $s);
	}
	$s = preg_replace("/[\ ]+/s", ' ', $s);
	$s = preg_replace("/[\n]+/s", "\n", $s);
	return $s;
}

function lib_global2_html_to_safe_str($s, $length) {
	$s = str_replace(array("\n", "\r", "\t", "\v", "\0", "\x0B"), '', preg_replace("/[^\x20-\xFF]/", " ", trim(@strval($s))));
   	$s = lib_global2_clean_xhtml($s);
	$s = lib_global2_remove_ents($s);
	$s = lib_global2_remove_tags($s);
	$s = lib_global2_ensure_lt_gt($s);
	$s = lib_global2_ord_space($s);
	if (!empty($length) && (mb_strlen($s, mb_detect_encoding($s)) > $length)) {
		$s = mb_substr($s, 0, ($length - 5), mb_detect_encoding($s)) . ' [...]';
	}
	$s = lib_global2_ensure_amp($s);
	return $s;
}

function lib_global2_mysql_table_exists($t) {

	$r = (mysql_query("SELECT 1 FROM `$t` WHERE 0")) ? true : false;

	return $r;
}

function lib_global2_mysql_get_column_value($tname, $cname, $idname, $idvalue) {
	$r = '';

	$q = @mysql_query("SELECT `$cname` FROM `$tname` WHERE `$idname`='$idvalue';");

	if (@mysql_num_rows($q) > 0) {
		while ($fa = @mysql_fetch_array($q, MYSQL_ASSOC)) {
			foreach ($fa as $k => $v) {
				if ($k == $cname) {
					$r = $v;
				}
			}
		}
	}

	return $r;
}

//dependent
function lib_global2_cut_option_value($s, $length) {
	$s = lib_global2_remove_ents($s);
	$s = lib_global2_remove_tags($s);
	$s = lib_global2_ord_space($s);
	if (mb_strlen($s, mb_detect_encoding($s)) > $length) {
		$s = mb_substr($s, 0, ($length - 3), mb_detect_encoding($s)) . '...&#160;';
	}
	$s = lib_global2_ensure_amp($s);
	return $s . '&#160;';
}

//dependent
function lib_global2_ensure_val_for_js($s) {
	return lib_global2_fix_str_for_js(lib_global2_ensure_amp(lib_global2_remove_tags($s)));
}

function lib_global2_date_to_time($s) {
	list($d, $t) = explode(' ', $s);
	list($y, $mon, $day) = explode('-', $d);
	list($h, $min, $sec) = explode(':', $t);
	$r = mktime($h, $min, $sec, $mon, $day, $y);
	return $r;
}

function lib_global2_to_rudate($t) {
	if (ctype_digit($t) && strlen($t) == 10) {
		$r_a_w = array('0' => '&#x432;&#x43e;&#x441;&#x43a;&#x440;&#x435;&#x441;&#x435;&#x43d;&#x44c;&#x435;', '1' => '&#x43f;&#x43e;&#x43d;&#x435;&#x434;&#x435;&#x43b;&#x44c;&#x43d;&#x438;&#x43a;', '2' => '&#x432;&#x442;&#x43e;&#x440;&#x43d;&#x438;&#x43a;', '3' => '&#x441;&#x440;&#x435;&#x434;&#x430;', '4' => '&#x447;&#x435;&#x442;&#x432;&#x435;&#x440;&#x433;', '5' => '&#x43f;&#x44f;&#x442;&#x43d;&#x438;&#x446;&#x430;', '6' => '&#x441;&#x443;&#x431;&#x431;&#x43e;&#x442;&#x430;');
		$r_a_m = array('01' => '&#x44f;&#x43d;&#x432;&#x430;&#x440;&#x44f;', '02' => '&#x444;&#x435;&#x432;&#x440;&#x430;&#x43b;&#x44f;', '03' => '&#x43c;&#x430;&#x440;&#x442;&#x430;', '04' => '&#x430;&#x43f;&#x440;&#x435;&#x43b;&#x44f;', '05' => '&#x43c;&#x430;&#x44f;', '06' => '&#x438;&#x44e;&#x43d;&#x44f;', '07' => '&#x438;&#x44e;&#x43b;&#x44f;', '08' => '&#x430;&#x432;&#x433;&#x443;&#x441;&#x442;&#x430;', '09' => '&#x441;&#x435;&#x43d;&#x442;&#x44f;&#x431;&#x440;&#x44f;', '10' => '&#x43e;&#x43a;&#x442;&#x44f;&#x431;&#x440;&#x44f;', '11' => '&#x43d;&#x43e;&#x44f;&#x431;&#x440;&#x44f;', '12' => '&#x434;&#x435;&#x43a;&#x430;&#x431;&#x440;&#x44f;',);
		$r_w = date("w", $t);
		$r_m = date("m", $t);
		$r_t = date("H:i", $t);
		$r_y = date("Y", $t);
		$r_d = date("d", $t);
		$r_w = $r_a_w["$r_w"];
		$r_a_m = $r_a_m["$r_m"];
		$r_date = $r_w . ', ' . $r_d . ' ' . $r_a_m . ' ' . $r_y . ' &#x433;&#x43e;&#x434;&#x430;';
		return $r_date;
	}
}

function lib_global2_make_clickable_links($s) {
	$in=array(
	'`(https?://([-\w\.]+)+(:\d+)?(/([\-\(\)\w/_\.]*(\?\S+)?)?)?)`si',
	'`([A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4})`si'
	);
	$out=array(
	'<a href="$1">$1</a> ',
	'<a href="mailto:$1">$1</a>'
	);
	return preg_replace($in,$out,$s);
}

function lib_global2_random_anchor() {
	$r = range(0, 9);
	shuffle($r);
	$ds = '';
	foreach ($r as $d) {
		$ds .= $d;
	}
	return $ds;
}

